package net.lcheng.sellerCenter.controller;

import net.lcheng.model.User;
import net.lcheng.model.WxShop;
import net.lcheng.model.WxShopWeixinConfig;
import net.lcheng.service.UserService;
import net.lcheng.service.WxShopService;
import net.lcheng.service.WxShopWeixinConfigService;
import net.lcheng.vo.WxShopWeixinConfigModel;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.validation.Valid;
import java.util.Date;
import java.util.UUID;

/**
 * Created by zwr on 2016/7/5 0005.
 */
@Controller
public class WxPublicNumberController {
    @Autowired
    private UserService userService;
    @Autowired
    private WxShopService wxShopService;
    @Autowired
    private WxShopWeixinConfigService wxShopWeixinConfigService;
  /**微信公众号绑定Get*/
    @RequestMapping(value = "wx/public_number", method = RequestMethod.GET)
    public String wxPublicNumberGet(Model model) {
        model.addAttribute("WxShopWeixinConfigModel", new WxShopWeixinConfigModel());
        Subject currentUser = SecurityUtils.getSubject();
        String userPhoneNum = currentUser.getSession().getAttribute("UserPhoneNum").toString();
        //获取用户信息
        User user = userService.getUserByPhone(userPhoneNum);
        WxShopWeixinConfig wxShopWeixinConfig = wxShopWeixinConfigService.getWxShopWeixinConfigByUserId(user.getUserId());
        //判断用户有没有进行过微信公众号有没有绑定
        if (wxShopWeixinConfig != null) {
            model.addAttribute("wxShopWeixinConfig", 1);
        }
        return "public_number";
    }
    /**微信公众号绑定Post*/
    @RequestMapping(value = "wx/public_number", method = RequestMethod.POST)
    public String wxPublicNumberPost(Model model, @Valid @ModelAttribute("WxShopWeixinConfigModel") WxShopWeixinConfigModel wxShopWeixinConfigModel, BindingResult result) {
        if (result.hasErrors()) {
            return "public_number";
        }
        Subject currentUser = SecurityUtils.getSubject();
        String userPhoneNum = currentUser.getSession().getAttribute("UserPhoneNum").toString();
        //获取用户信息
        User user = userService.getUserByPhone(userPhoneNum);
        //获取店铺id
        WxShop shop = wxShopService.getShopInforByUserId(user.getUserId());
        WxShopWeixinConfig wxShopWeixinConfig = new WxShopWeixinConfig();
        wxShopWeixinConfig.setId(UUID.randomUUID().toString());
        wxShopWeixinConfig.setName(wxShopWeixinConfigModel.getName());
        wxShopWeixinConfig.setPublicNumberId(wxShopWeixinConfigModel.getPublicNumberId());
        wxShopWeixinConfig.setWeixinNumber(wxShopWeixinConfigModel.getWeixinNumber());
        wxShopWeixinConfig.setPublicNumber(wxShopWeixinConfig.getWeixinNumber());
        wxShopWeixinConfig.setAppSecret(wxShopWeixinConfigModel.getAppSecret());
        wxShopWeixinConfig.setType(wxShopWeixinConfigModel.getType());
        wxShopWeixinConfig.setShopId(shop.getShopId());
        wxShopWeixinConfig.setUserId(user.getUserId());
        wxShopWeixinConfig.setCreateOn(new Date());
        int returnResult = wxShopWeixinConfigService.addConfig(wxShopWeixinConfig);
        if (returnResult == 1) {
            model.addAttribute("result", 1);
        }
        return "public_number";
    }
}
